<?php
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.modellist');
class RaovatModelSearch extends JModelLegacy{
	
	protected $_data = null;
	protected $_menu = null;
	
	protected $_total = null;
	protected $_pagination = null;
	public function __construct()
	{
		parent::__construct();
		$app    = JFactory::getApplication();
		$config = JFactory::getConfig();
		$this->setState('limit', $app->getUserStateFromRequest('com_search.limit', 'limit', $config->get('list_limit'), 'uint'));
		$this->setState('limitstart', $app->input->get('limitstart', 0, 'uint'));
		$this->setState('region', $app->input->get('region', 0, 'uint'));
		$this->setState('search', $app->input->get('search','','string'));
	}
	
	public function getData()
	{
		if (empty($this->_data))
		{
			$db = JFactory::getDBO();
			$query = $db->getQuery(true);
			$query->select('#__raovat.*');
			$query->from('#__raovat');
			$query->where('published = 1');
			
			$search = $this->getState('search');
			if ($search){
				$search = $db->quote('%' . $db->escape($search, true) . '%', false);
				$query->where('#__raovat.title LIKE ' . $search );
			}
			$region = $this->getState('region');
			if ($region){
				$query->where('#__raovat.region= ' . $db->escape($region));
			}
			$query->order('id desc');
			
			$db->setQuery((string) $query,$this->getState('limitstart'),$this->getState('limit'));
			$this->_data = $db->loadObjectList();
		}
		return $this->_data;
	}
	
	public function getTotal()
	{
		$db = JFactory::getDBO();
		$query = $db->getQuery(true);
		$query->select('count(*)');
		$query->from('#__raovat');
		$query->where('published = 1');
		
		$search = $this->getState('search');
		if ($search){
			$search = $db->quote('%' . $db->escape($search, true) . '%');
			$query->where('#__raovat.title LIKE ' . $search );
		}
		$region = $this->getState('region');
		if ($region){
			$query->where('#__raovat.region= ' . $db->escape($region));
		}
		
		$db->setQuery((string) $query);
		return $this->_total = $db->loadResult();
	}

	public function getPagination()
	{
		// Lets load the content if it doesn't already exist
		if (empty($this->_pagination))
		{
			$this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit'));
		}
		return $this->_pagination;
	}
	
}
